package cn.iocoder.yudao.module.biz.service.risk.equipmentInfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.risk.equipmentInfo.vo.*;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.BizTaskWorkCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.BizTaskWorkExcelVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.BizTaskWorkImportRespVO;
import cn.iocoder.yudao.module.biz.convert.risk.equipmentInfo.BizEquipmentInfoConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.equipmentInfo.BizEquipmentInfoDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.equipmentInfo.BizEquipmentInfoMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;

/**
 * 设备 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizEquipmentInfoServiceImpl implements BizEquipmentInfoService {

    @Resource
    private BizEquipmentInfoMapper equipmentInfoMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    public String createEquipmentInfo(BizEquipmentInfoCreateReqVO createReqVO) {
        // 插入
        BizEquipmentInfoDO equipmentInfo = BizEquipmentInfoConvert.INSTANCE.convert(createReqVO);
        equipmentInfo.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        equipmentInfoMapper.insert(equipmentInfo);
        // 返回
        return equipmentInfo.getId();
    }

    @Override
    public void updateEquipmentInfo(BizEquipmentInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEquipmentInfoExists(updateReqVO.getId());
        // 更新
        BizEquipmentInfoDO updateObj = BizEquipmentInfoConvert.INSTANCE.convert(updateReqVO);
        equipmentInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentInfo(String id) {
        // 校验存在
        this.validateEquipmentInfoExists(id);
        // 删除
        equipmentInfoMapper.deleteById(id);
    }

    private void validateEquipmentInfoExists(String id) {
        if (equipmentInfoMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BizEquipmentInfoDO getEquipmentInfo(String id) {
        return equipmentInfoMapper.selectById(id);
    }

    @Override
    public List<BizEquipmentInfoDO> getEquipmentInfoList(Collection<String> ids) {
        return equipmentInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizEquipmentInfoDO> getEquipmentInfoPage(BizEquipmentInfoPageReqVO pageReqVO) {
        pageReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return equipmentInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizEquipmentInfoDO> getEquipmentInfoList(BizEquipmentInfoExportReqVO exportReqVO) {
        exportReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return equipmentInfoMapper.selectList(exportReqVO);
    }

    @Override
    public Long getEquipmentCount() {
        return equipmentInfoMapper.selectCount(new QueryWrapperX<BizEquipmentInfoDO>().eqIfPresent("deleted",0));
    }

    @Override
    public List<Map<String, Object>> getEquipmentPie() {
        BizEquipmentInfoDO equipmentInfoDO = new BizEquipmentInfoDO();
        equipmentInfoDO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return equipmentInfoMapper.getEquipmentPie(equipmentInfoDO);
    }

    @Override
    public List<BizEquipmentInfoRespVO> getEquipmentPage(BizEquipmentInfoPageReqVO pageReqVO) {
        Integer pageNo=pageReqVO.getPageNo();
        Integer pageSize=  pageReqVO.getPageSize();
        //设置为limit开始的条数
        pageReqVO.setPageNo((pageNo-1)*pageSize);
        pageReqVO.setDeptId(SecurityFrameworkUtils.getLoginUser().getDeptId()+"");
        return equipmentInfoMapper.getEquipmentPage(pageReqVO);
    }

    @Override
    public Integer getEquipmentInfoCount(BizEquipmentInfoPageReqVO pageReqVO) {
        return equipmentInfoMapper.getEquipmentInfoCount(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public BizEquipmentImportRespVO importExcel(List<BizEquipmentInfoExcelVO> importEquipment) {
        if (CollUtil.isEmpty(importEquipment)) {
            throw exception(EQUIPMENT_INFO_LIST_IS_EMPTY);
        }
        BizEquipmentImportRespVO respVO = BizEquipmentImportRespVO.builder().failureEquipmentnames(new LinkedHashMap<>()).build();
        Map<String,String> map = new HashMap<>();
        for(BizEquipmentInfoExcelVO vo : importEquipment){
            BizEquipmentInfoCreateReqVO equipmentInfoCreateReqVO = new BizEquipmentInfoCreateReqVO();
            equipmentInfoCreateReqVO.setEquipmentName(vo.getEquipmentName());
            equipmentInfoCreateReqVO.setEquipmentModel(vo.getEquipmentModel());
            equipmentInfoCreateReqVO.setEquipmentType(vo.getEquipmentType());
            equipmentInfoCreateReqVO.setIsSpecial(vo.getIsSpecial());
            equipmentInfoCreateReqVO.setLocation(vo.getLocation());
            equipmentInfoCreateReqVO.setEquipmentCount(vo.getEquipmentCount());
            equipmentInfoCreateReqVO.setRemarks(vo.getRemarks());
            createEquipmentInfo(equipmentInfoCreateReqVO);
        }
        return respVO;
    }

    @Override
    public List<BizEquipmentInfoExcelVO> getEquipmentInfoForExport(BizEquipmentInfoExportReqVO exportReqVO) {
        exportReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return equipmentInfoMapper.getEquipmentInfoForExport(exportReqVO);
    }

}
